package com.tcl.zenglx.file.config;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Data
@Component
public class MinIoClientConfig {
    @Value("${minio.endpoint}")
    private String endpoint;
    @Value("${minio.accessKey}")
    private String accessKey;
    @Value("${minio.secretKey}")
    private String secretKey;
    @Value("${minio.bucketName}")
    private String bucketName;
    @Value("${minio.tmpBucketName}")
    private String tmpBucketName;
 
    /**
     * 注入minio 客户端
     * @return
     */
    @Bean
    public MinioClient minioClient() throws Exception{
        MinioClient minioClient = MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();

        if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }
        if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(tmpBucketName).build())) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(tmpBucketName).build());
        }
        return  minioClient;
    }
 
}